﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ForumServer.Logger;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using ForumServer.Forum;
using log4net;

namespace ForumClient
{
    class ClientConfiguration
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="auth"></param>
        /// <param name="forum"></param>
        public static void configure(ref ILog logger, ref Authenticator auth, ref Forum forum)
        {
            Guid clientID = Guid.NewGuid();
            Console.Title = "Remote Client - ID - " + clientID.ToString();
            Logger log = new Logger(clientID, "Client");
            logger = log.getLogger();
            if (auth != null)
            {
                configureReLogin(ref forum, logger);
                return;
            }

            //------------------------Client Configuration------------------------//
            TcpChannel x_channel = new TcpChannel();
            try
            {
                ChannelServices.RegisterChannel(x_channel, false);
            }
            catch
            {
                logger.Fatal("Failed to register to tcp channel");
            }

            //------------------------Activate the objects that were published on the server------------------------//           
            try
            {
                RemotingConfiguration.RegisterActivatedClientType(typeof(User), "tcp://localhost:20172");
                RemotingConfiguration.RegisterActivatedClientType(typeof(Moderator), "tcp://localhost:20172");
                RemotingConfiguration.RegisterActivatedClientType(typeof(ForumServer.Forum.Thread), "tcp://localhost:20172");
                RemotingConfiguration.RegisterActivatedClientType(typeof(Post), "tcp://localhost:20172");
                logger.Info("Registered classes: User, Thread, Post");
            }
            catch
            {
                logger.Fatal("Failed to register a published class to initialize");
            }
            //------------------------Retreive the Authenticator so it can be accessed on the client------------------------//
            
            try
            {
                auth = (Authenticator)Activator.GetObject(typeof(Authenticator), "tcp://localhost:20172/Authenticator");
                logger.Info("Retrieved Authenticator class");
            }
            catch
            {
                logger.Fatal("Failed to retrieve published class Authenticator");
            }

            //------------------------Retreive the forum object so it can be accessed on the client------------------------//
            
           /* try
            {
                forum = (Forum)Activator.GetObject(typeof(Forum), "tcp://localhost:20172/Forum");
                logger.Info("Retrieved Forum class");
            }
            catch
            {
                logger.Fatal("Failed to retrieve published class Forum");
            }*/
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="auth"></param>
        /// <param name="forum"></param>
        public static void disconnectConfigure(ref Forum forum)
        {
            forum = null;
        }

        /// <summary>
        /// client login after logout
        /// </summary>
        /// <param name="forum"></param>
        /// <param name="logger"></param>
        public static void configureReLogin(ref Forum forum, ILog logger)
        {
            try
            {
                forum = (Forum)Activator.GetObject(typeof(Forum), "tcp://localhost:20172/Forum");
                logger.Info("Retrieved Forum class");
            }
            catch
            {
                logger.Fatal("Failed to retrieve published class Forum");
            }
        }
        /// <summary>
        /// client login after logout
        /// </summary>
        /// <param name="forum"></param>
        /// <param name="logger"></param>
        public static void configureLoginToForum(ref Forum forum, ILog logger,int index)
        {
            try
            {
                forum = (Forum)Activator.GetObject(typeof(Forum), "tcp://localhost:20172/Forum" + index);
                logger.Info("Retrieved Forum class");
            }
            catch
            {
                logger.Fatal("Failed to retrieve published class Forum");
            }
        }

    }
}
